package tyk.Servlet;

import tyk.Service.BookService;
import tyk.pojo.AccountPage;
import tyk.pojo.Book;
import tyk.pojo.Page;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.List;

/***
 *@title $NAME
 *@description <TODO description class purpose>
 *@author Tang'ya'kang
 *@VERSION 1.0.0
 *@CREATE 2023/5/13 21:44
 **/
public class ClientBookServlet extends HttpServlet {
    public void PageByPageNo(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        AccountPage accountPage=new AccountPage();
        BookService bookService = new BookService();    //一次性将所有图书信息分页展示出来
        Integer Page= Integer.valueOf(request.getParameter("Page"));
        Integer pageTotalCount=bookService.SelectEvery().size();
        accountPage.setCount(pageTotalCount);
        Integer pageTotal=pageTotalCount/10;
        if(pageTotalCount%10!=0) pageTotal++;
        accountPage.setPageTocal(pageTotal);
        if(Page<1) Page=1;
        else if(Page>pageTotal) Page=pageTotal;
        accountPage.setPage(Page);
        List<Book> list=bookService.PageByPageNo(Page,10);
        request.setAttribute("books",list);
        request.setAttribute("AccountPage",accountPage);
        request.setAttribute("url","./client/bookServlet?action=PageByPageNo&Page=");
        request.getRequestDispatcher("/html/home.jsp").forward(request,response);
    }

    public void SelectBookBySales(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, SQLException, NoSuchFieldException, IllegalAccessException, InstantiationException {
        List<Book> list;
        BookService bookService=new BookService();
        list=bookService.SelectBookBySales();
        request.setAttribute("BookbySales",list);
        request.getRequestDispatcher("/html/FirstHome.jsp").forward(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String hidden=request.getParameter("action");
        try {
            Method method = this.getClass().getDeclaredMethod(hidden,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
